package com.jy.wms.controller;


import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import com.jy.wms.service.WmsFaYunOrderMxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO(发运订单明细导出)
 */

@RestController
@RequestMapping("/wmsFaYunOrderMx")
@Api(value = "/wmsFaYunOrderMx", description = "发运订单明细导出")
public class WmsFaYunOrderMxController extends BaseController<WmsFaYunOrderMxService, WmsShipmentOrder> {



    @ApiOperation(value = "导出发运订单明细", response = JSONObject.class, notes = "导出发运订单明细")
    @PostMapping(value = "getOutFaYunOrderList")
    public JSONObject getOutFaYunOrderList(HttpServletResponse response, @RequestBody Map map, HttpSession session) throws IOException {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String fybh = (String) map.get("fybh");//so编号
        if (fybh != null && fybh != "") {
            map.put("fybh", fybh);
        }
        String fylx = (String) map.get("fylx");//类型
        if (fylx != null && fylx != "") {
            map.put("fylx", fylx);
        }
        String khbh = (String) map.get("khbh");//客户编号
        if (khbh != null && khbh != "") {
            map.put("khbh", khbh);
        }
        String khdj = (String) map.get("khdj");
        if (khdj != null && khdj != "") {
            map.put("khdj", khdj);
        }
        String khmc = (String) map.get("khmc");//客户名称
        if (khmc != null && khmc != "") {
            map.put("khmc", khmc);
        }
        String bczt = (String) map.get("bczt");
        if (bczt != null && bczt != "") {
            map.put("bczt", bczt);
        }

        String khdz = (String) map.get("khdz");
        if (khdz != null && khdz != "") {
            map.put("khdz", khdz);
        }
        String khddh = (String) map.get("khddh");
        if (khddh != null && khddh != "") {
            map.put("khddh", khddh);
        }
        String cdzt = (String) map.get("cdzt");
        if (cdzt != null && cdzt != "") {
            map.put("cdzt", cdzt);
        }
        String zxzt = (String) map.get("zxzt");
        if (zxzt != null && zxzt != "") {
            map.put("zxzt", zxzt);
        }
        String jhzt = (String) map.get("jhzt");
        if (jhzt != null && jhzt != "") {
            map.put("jhzt", jhzt);
        }
        String shzt = (String) map.get("shzt");
        if (shzt != null && shzt != "") {
            map.put("shzt", shzt);
        }
        String fyzt = (String) map.get("fyzt");
        if (fyzt != null && fyzt != "") {
            map.put("fyzt", fyzt);
        }
        String xl = (String) map.get("xl");
        if (xl != null && xl != "") {
            map.put("xl", xl);
        }
        String lxrxm = (String) map.get("lxrxm");
        if (lxrxm != null && lxrxm != "") {
            map.put("lxrxm", lxrxm);
        }
        String yxj = (String) map.get("yxj");
        if (yxj != null && yxj != "") {
            map.put("yxj", yxj);
        }

        if(map.containsKey("khcjsj_start")){
            Date date =new Date((Long)map.get("khcjsj_start"));
            map.put("khcjsj_start",format.format(date));
        }
        Calendar c3=Calendar.getInstance();
        if (map.containsKey("khcjsj_end")) {
            c3.setTime(new Date((Long)map.get("khcjsj_end")));
            c3.set(Calendar.HOUR_OF_DAY, 23);
            c3.set(Calendar.MINUTE, 59);
            c3.set(Calendar.SECOND, 59);
            map.put("khcjsj_end",format.format(c3.getTime()));
        }
        if(map.containsKey("cjsj_start")){
            Date date =new Date((Long)map.get("cjsj_start"));
            map.put("cjsj_start",format.format(date));
        }
        Calendar c2=Calendar.getInstance();
        if (map.containsKey("cjsj_end")) {
            c2.setTime(new Date((Long)map.get("cjsj_end")));
            c2.set(Calendar.HOUR_OF_DAY, 23);
            c2.set(Calendar.MINUTE, 59);
            c2.set(Calendar.SECOND, 59);
            map.put("cjsj_end",format.format(c2.getTime()));
        }

        if(map.containsKey("jhfhsj_start")){
            Date date =new Date((Long)map.get("jhfhsj_start"));
            map.put("jhfhsj_start",format.format(date));
        }
        Calendar c1=Calendar.getInstance();
        if (map.containsKey("jhfhsj_end")) {
            c1.setTime(new Date((Long)map.get("jhfhsj_end")));
            c1.set(Calendar.HOUR_OF_DAY, 23);
            c1.set(Calendar.MINUTE, 59);
            c1.set(Calendar.SECOND, 59);
            map.put("jhfhsj_end",format.format(c1.getTime()));
        }
        if(map.containsKey("yqjhsj_start")){
            Date date =new Date((Long)map.get("yqjhsj_start"));
            map.put("yqjhsj_start",format.format(date));
        }
        Calendar c=Calendar.getInstance();
        if (map.containsKey("yqjhsj_end")) {
            c.setTime(new Date((Long)map.get("yqjhsj_end")));
            c.set(Calendar.HOUR_OF_DAY, 23);
            c.set(Calendar.MINUTE, 59);
            c.set(Calendar.SECOND, 59);
            map.put("yqjhsj_end",format.format(c.getTime()));
        }
        String hpmc = (String) map.get("hpmc");
        if (hpmc != null && hpmc != "") {
            map.put("hpmc", hpmc);
        }
        String hpbh = (String) map.get("hpbh");
        if (hpbh != null && hpbh != "") {
            map.put("hpbh", hpbh);
        }
        String jhfs = (String) map.get("jhfs");
        if (jhfs != null && jhfs != "") {
            map.put("jhfs", jhfs);
        }
        String bcbh = (String) map.get("bcbh");
        if (bcbh != null && bcbh != "") {
            map.put("bcbh", bcbh);
        }
        String ckzd1 = (String) map.get("ckzd1");
        if (ckzd1 != null && ckzd1 != "") {
            map.put("ckzd1", ckzd1);
        }
        String ckzd2 = (String) map.get("ckzd2");
        if (ckzd2 != null && ckzd2 != "") {
            map.put("ckzd2", ckzd2);
        }
        String ckzd3 = (String) map.get("ckzd3");
        if (ckzd3 != null && ckzd3 != "") {
            map.put("ckzd3", ckzd3);
        }
        String ckzd4 = (String) map.get("ckzd4");
        if (ckzd4 != null && ckzd4 != "") {
            map.put("ckzd4", ckzd4);
        }
        String ckzd5 = (String) map.get("ckzd5");
        if (ckzd5 != null && ckzd5 != "") {
            map.put("ckzd5", ckzd5);
        }
        String ddqdmc = (String) map.get("ddqdmc");
        if (ddqdmc != null && ddqdmc != "") {
            map.put("ddqdmc", ddqdmc);
        }
        String cyslb = (String) map.get("cyslb");
        if (cyslb != null && cyslb != "") {
            map.put("cyslb", cyslb);
        }

        String filename = "发运订单明细导出";
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        String hzid =  hz.get(0).getHzid();
        map.put("ckid", ckid);
        map.put("hzid", hzid);

        List<String> ids = (List) map.get("ids");
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("发运订单表", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<WmsFaYunOrderMx> list = null;
        list = baseService.getOutFaYunOrderList(ids, map);

        Workbook workbook = ExcelExportUtil.exportExcel(ep, WmsFaYunOrderMx.class, list);
        workbook.write(response.getOutputStream());
        return null;
    }



}
